import sys
import os

current_dir = os.path.dirname(os.path.abspath(__file__))
grandparent_dir = os.path.dirname(os.path.dirname(current_dir))
sys.path.append(grandparent_dir)

import random
import numpy as np
import time
import pickle
from object.People import People
import matplotlib.pyplot as plt
random.seed(int(time.time()))
# 参数区
speed_max = 1.2         # 平路运动最高速 (m/s)
speed_min = 0.8         # 平路运动最低速 (m/s)
people_per_floor = 100   # 每层初始人数

with open("data/Building_generate_0430.pkl", "rb") as haddle:
    building = pickle.load(haddle)

# 建筑位置状态
# 0表示空地
# 1表示障碍物或墙
# 2表示上楼梯位置
# 3表示下楼梯位置
# 4表示出口

# 初始化人员
peoples = []
for f in range(building.num_floors):
    count = 0
    while count < people_per_floor:
        i,j = random.randint(1,building.floor_width-2), random.randint(1, building.floor_length-2)
        if building.floors[f,i,j] in [0,2,3]:  # 空地或楼梯可以初始放人
            people = People(f, i, j, random.uniform(speed_min,speed_max))
            peoples.append(people)
            count +=1

with open("data/People_generate_0430.pkl", "wb") as haddle:
    pickle.dump(peoples, haddle)

# 画建筑物结构及人员位置
fig, axes = plt.subplots(1, building.num_floors, figsize=(5*building.num_floors,5))
plt.tight_layout()

for f in range(building.num_floors):
    ax = axes[f]
    ax.set_title(f'Floor {f+1}')
    ax.set_xticks([]); ax.set_yticks([])
    np.where 
    # 障碍物或墙灰色方块
    pos_s = np.where(building.floors[f] == 1)       # 1代表障碍物或墙
    for i, j in zip(pos_s[0], pos_s[1]):
        i = int(i)
        j = int(j)
        ax.plot(i, j, 's', color='#808080', markersize=3, label='Wall or Obstable')
    # 上楼梯口蓝色标记
    pos_s = np.where(building.floors[f] == 2)       # 2代表上楼梯 
    for i, j in zip(pos_s[0], pos_s[1]):
        i = int(i)
        j = int(j)    
        ax.plot(i, j, 'b^', markersize=3, label='Upstairs')
    # 下楼梯口绿色标记        
    pos_s = np.where(building.floors[f] == 3)       # 3代表下楼梯 
    for i, j in zip(pos_s[0], pos_s[1]):
        i = int(i)
        j = int(j)  
        ax.plot(i, j, 'gv', markersize=3, label='Downstairs')      
    # 出口红色方块
    pos_s = np.where(building.floors[f] == 4)       # 4代表出口
    for i, j in zip(pos_s[0], pos_s[1]):
        i = int(i)
        j = int(j)      
        ax.plot(i, j, 'rs', markersize=3, label='Exit')
    # 人员红色点
    for people in peoples:
        if people.pos[0] == f:
            ax.plot(people.pos[1],people.pos[2],'r.')  

plt.savefig("data/People_generate_0430.jpg")
